//
//  YYLWellcomeViewController.swift
//  XinlangWeibo
//
//  Created by yangyinglei on 2017/3/30.
//  Copyright © 2017年 yangyinglei. All rights reserved.
//

import UIKit
import SDWebImage

class YYLWellcomeViewController: UIViewController {

    //  头像图片
    lazy var headImage: UIImageView = {
        let image = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        if let imgUrl = YYLUserAccountViewModel.shareViewModel.userAccount?.avatar_large {
            image.sd_setImage(with: URL(string: imgUrl), placeholderImage: UIImage(named: "avatar_default_big"))
        
        }
        image.layer.cornerRadius = 45
        image.layer.masksToBounds = true
        return image
    }()
    //  背景图片
    lazy var imageView = UIImageView(image: UIImage(named: "ad_background"))
    //  欢迎文字
    lazy var wellcomeLable: UILabel? = {
        let label = UILabel(text: "欢迎回来", font: 16, color: UIColor.darkGray)
        if let name = YYLUserAccountViewModel.shareViewModel.userAccount?.name {
            label.text = "欢迎回来，\(name)"

        }
    return label
    }()
    //加载视图
    override func loadView() {
        
        view = imageView
        
    }
    //视图加载完成
    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
        
    }

    //视图已经显示
    override func viewDidAppear(_ animated: Bool) {
        startAnimation()
    }
    
    func setupUI() {
        view.addSubview(headImage)
        view.addSubview(wellcomeLable!)


        headImage.snp.makeConstraints { (make)in
            make.centerX.equalTo(imageView)
            make.top.equalTo(imageView).offset(200)
            make.size.equalTo(CGSize(width: 90, height: 90))
        }
        
        wellcomeLable!.snp.makeConstraints({ (make) in
            make.centerX.equalTo(imageView)
            make.top.equalTo(headImage.snp.bottom).offset(15)
            
        })
        
        
    }
    
    //头像动画
    func startAnimation() {
        headImage.snp.updateConstraints { (make) in
            make.top.equalTo(imageView).offset(150)
        }
        self.wellcomeLable?.alpha = 0
        
        UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 20, options: [], animations: {
            self.view.layoutIfNeeded()
            
        }) { (_) in
            
            UIView.animate(withDuration: 1, delay: 0, options: [], animations: {
                self.wellcomeLable?.alpha = 1
            }, completion: { (_) in
                //欢迎界面消失  切换到主页面
                NotificationCenter.default.post(name: NSNotification.Name(rawValue: SwitchRootVCNotification), object: nil)
                
            })
        }
    }
 
}
